package test.jb698.codec.msm.library;

import java.io.Reader;
import java.io.StringReader;

import com.alibaba.fastjson.JSONObject;

import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;

/**
 * @author Administrator(LiuJianbin)
 * @date 2019年10月25日 下午9:34:06
 * 
 */
public class MainJsqlparser {

    public static void main(String[] args) throws Exception {

        String sql = "SELECT ta.Column11 , ta.column12 ,tb.column21 as tbc21 , tb.column22  FROM TABLEA ta LEFT JOIN TABLEB as tb  WHERE 1=1 /* 注释内容 */ " ;
        sql = "SELECT ta.column_11 , ta.column_12 ,tb.column_21 as tbc21 , tb.column22  FROM table_name_a ta LEFT JOIN table_name_b as tb  WHERE 1=1 /* 注释内容 */ " ;

        CCJSqlParserManager sqpm = new CCJSqlParserManager();

        Reader statementReader = new StringReader(sql); 
        Statement parseStmt = sqpm.parse(statementReader ); 

        if(parseStmt instanceof Select) {
            Select selectStmt = (Select)parseStmt;
//            List<WithItem> withItemsList = selectStmt.getWithItemsList();
            System.err.println(JSONObject.toJSONString(selectStmt));
            // {"selectBody":{"aSTNode":{"id":6},"forUpdate":false,"fromItem":{"aSTNode":{"id":5},"alias":{"name":"ta","useAs":false},"database":{"fullyQualifiedName":""},"fullyQualifiedName":"TABLEA","name":"TABLEA"},"joins":[{"aSTNode":{"id":10},"cross":false,"full":false,"inner":false,"left":true,"natural":false,"outer":false,"right":false,"rightItem":{"aSTNode":{"id":5},"alias":{"name":"tb","useAs":true},"database":{"fullyQualifiedName":""},"fullyQualifiedName":"TABLEB","name":"TABLEB"},"semi":false,"simple":false,"straight":false,"windowJoin":false}],"mySqlSqlCalcFoundRows":false,"mySqlSqlNoCache":false,"oracleSiblings":false,"selectItems":[{"aSTNode":{"id":9},"expression":{"aSTNode":{"id":4},"columnName":"Column11","fullyQualifiedName":"ta.Column11","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"ta","name":"ta"}}},{"aSTNode":{"id":9},"expression":{"aSTNode":{"id":4},"columnName":"column12","fullyQualifiedName":"ta.column12","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"ta","name":"ta"}}},{"aSTNode":{"id":9},"alias":{"name":"tbc21","useAs":true},"expression":{"aSTNode":{"id":4},"columnName":"column21","fullyQualifiedName":"tb.column21","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"tb","name":"tb"}}},{"aSTNode":{"id":9},"expression":{"aSTNode":{"id":4},"columnName":"column22","fullyQualifiedName":"tb.column22","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"tb","name":"tb"}}}],"useBrackets":false,"where":{"aSTNode":{"id":14},"leftExpression":{"bigIntegerValue":1,"stringValue":"1","value":1},"oldOracleJoinSyntax":0,"oraclePriorPosition":0,"rightExpression":{"bigIntegerValue":1,"stringValue":"1","value":1},"stringExpression":"="}}}
            // {"selectBody":{"aSTNode":{"id":6},"forUpdate":false,"fromItem":{"aSTNode":{"id":5},"alias":{"name":"ta","useAs":false},"database":{"fullyQualifiedName":""},"fullyQualifiedName":"table_name_a","name":"table_name_a"},"joins":[{"aSTNode":{"id":10},"cross":false,"full":false,"inner":false,"left":true,"natural":false,"outer":false,"right":false,"rightItem":{"aSTNode":{"id":5},"alias":{"name":"tb","useAs":true},"database":{"fullyQualifiedName":""},"fullyQualifiedName":"table_name_b","name":"table_name_b"},"semi":false,"simple":false,"straight":false,"windowJoin":false}],"mySqlSqlCalcFoundRows":false,"mySqlSqlNoCache":false,"oracleSiblings":false,"selectItems":[{"aSTNode":{"id":9},"expression":{"aSTNode":{"id":4},"columnName":"column_11","fullyQualifiedName":"ta.column_11","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"ta","name":"ta"}}},{"aSTNode":{"id":9},"expression":{"aSTNode":{"id":4},"columnName":"column_12","fullyQualifiedName":"ta.column_12","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"ta","name":"ta"}}},{"aSTNode":{"id":9},"alias":{"name":"tbc21","useAs":true},"expression":{"aSTNode":{"id":4},"columnName":"column_21","fullyQualifiedName":"tb.column_21","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"tb","name":"tb"}}},{"aSTNode":{"id":9},"expression":{"aSTNode":{"id":4},"columnName":"column22","fullyQualifiedName":"tb.column22","table":{"database":{"fullyQualifiedName":""},"fullyQualifiedName":"tb","name":"tb"}}}],"useBrackets":false,"where":{"aSTNode":{"id":14},"leftExpression":{"bigIntegerValue":1,"stringValue":"1","value":1},"oldOracleJoinSyntax":0,"oraclePriorPosition":0,"rightExpression":{"bigIntegerValue":1,"stringValue":"1","value":1},"stringExpression":"="}}}
            // 找出所有查询的表.
        }
        

    }

}
